#include "recboxitem.h"
#include "ui_recboxitem.h"
#include <QPropertyAnimation>
#include <QDebug>
#include <string>

RecBoxItem::RecBoxItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecBoxItem)
{
    ui->setupUi(this);

    ui->musicImageBox->installEventFilter(this);
}

RecBoxItem::~RecBoxItem()
{
    delete ui;
}

void RecBoxItem::setText(const QString &text)
{
    ui->recBoxItemText->setText(text);

}

void RecBoxItem::setImage(const QString &Imagepath)
{
    QString imgStyle = "border-image:url("+Imagepath+");";
    ui->recMusicImage->setStyleSheet(imgStyle);
}

bool RecBoxItem::eventFilter(QObject *watched, QEvent *event)
{
    // 注意：不要忘记事件拦截器安装，否则时间拦截不到，因此需要在构造函数中添加：
    // 拦截事件处理器时，⼀定要安装事件拦截器

    // 注意：recItem上有⼀个按钮，当⿏标放在按钮上时在开启动画
    if (watched == ui->musicImageBox)
    {
        int ImgWidth = ui->musicImageBox->width();
        int ImgHeight = ui->musicImageBox->height();

//        (x,y)坐标可能跟QT设计师界面有差距，我们可以尝试打印出来。
//        qDebug() << ui->musicImageBox->x();
//        qDebug() << ui->musicImageBox->y();
        if(event->type() == QEvent::Enter) {
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox, "geometry");
            animation->setDuration(200);
            animation->setStartValue(QRect(13, 13, ImgWidth, ImgHeight));
            animation->setEndValue(QRect(13,0, ImgWidth, ImgHeight));
            animation->start();

            // 注意：动画结束的时候会触发finished信号，拦截到该信号，销毁animation
            connect(animation, &QPropertyAnimation::finished,this,[=]() {
                        delete  animation;
                        qDebug() << "图片上移动画结束";
                    });
            return true;
        }
        else if(event->type() == QEvent::Leave) {
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox, "geometry");
            animation->setDuration(200);
            animation->setStartValue(QRect(13,0, ImgWidth, ImgHeight));
            animation->setEndValue(QRect(13,13, ImgWidth, ImgHeight));
            animation->start();

            // 注意：动画结束的时候会触发finished信号，拦截到该信号，销毁animation
            connect(animation, &QPropertyAnimation::finished,this,[=]() {
                        delete  animation;
                        qDebug() << "图片下移动画结束";
                    });
            return true;
        }

    }

    return QWidget::eventFilter(watched, event);
}
